import { fileURLToPath } from 'url'
import { loadEnv, type ConfigEnv, type UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import mpa from 'vite-plugin-mpa'
import { viteMockServe } from 'vite-plugin-mock'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// import { createStyleImportPlugin, VxeTableResolve } from 'vite-plugin-style-import'
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
  const root = process.cwd()
  const prodMock = true
  const env = loadEnv(mode, root)
  const isBuild = command === 'build'
  return {
    plugins: [
      // have to
      vue(),
      // have to
      vueJsx(),
      // support name
      // vueSetupExtend()

      Components({
        resolvers: [ElementPlusResolver(), IconsResolver()] // 自动注册图标组件
      }),
      Icons(),
      mpa(),
      AutoImport({
        imports: ['vue', 'vue-router', 'pinia'],
        resolvers: [ElementPlusResolver()]
      }),
      viteMockServe({
        mockPath: 'mock',
        localEnabled: command === 'serve',
        prodEnabled: command !== 'serve' && prodMock
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('src', import.meta.url)),

        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js' //为了解决vue-i18n在浏览器上的告警
      }
    },
    build: {
      assetsInlineLimit: 4096000, // 小于4000kb的图片就转换成base64 字符直接放在你引用这张图片的文件中
      emptyOutDir: true // 打包前清除输出目录中的所有文件
    }
    // server: {
    //   proxy: {
    //     '/dev': {
    //       target: 'http://127.0.0.1:3001',
    //       changeOrigin: true,
    //       rewrite: path => path.replace(/^\/dev/, '')
    //     }
    //   }
    // }
  }
}
